<template>
  <div class="container">
    <div class="btnList">
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
    </div>
    <div class="btnList">
      期初时间
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date1"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="期初开始时间"
        end-placeholder="期初结束日期"
      ></el-date-picker>
    </div>
    <div class="btnList">
      期末时间
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date2"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="期末开始时间"
        end-placeholder="期末结束日期"
      ></el-date-picker>
    </div>
    <div class="top">
      <el-select v-model="search.level" placeholder="请选择商品分类等级">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <el-cascader
        v-model="search.orgAreaId"
        placeholder="请选择区域"
        :options="treeData"
        :props="defaultProps"
      ></el-cascader>

      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="orgAreaName"
        label="机构区域名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="orgName"
        label="机构名称"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="productClassName"
        label="商品分类名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sumProfit"
        label="销售毛利"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sumMoney"
        label="销售金额"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="sumCost"
        label="销售成本"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sumQty"
        label="销售数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="profitRate"
        label="毛利率"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { getCheckSalesProfit } from "@/api/datatable/grossMarginReport";
import { getToken } from "@/utils/auth";
import { getnowDate } from "@/utils/index";
import { getOrgAreaInfoSelect } from "@/api/publicChart";

export default {
  components: {
    pagination: Pagination
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      date: [],
      date1: [],
      date2: [],
      search: {
        start_date: "",
        end_date: "",
        beginStartTime: "",
        beginEndTime: "",
        orveStartTime: "",
        overEndTime: "",
        level: 1,
        orgAreaId: ["2", "3", "4"]
      },
      treeData: [],
      defaultProps: {
        children: "data",
        label: "name",
        value: "id"
      },

      options: [
        {
          value: 1,
          label: "一级分类"
        },
        { value: 2, label: "二级分类" },
        { value: 3, label: "三级分类" },
        { value: 4, label: "商品信息" }
      ],
      //默认时间
      normal_startime: getnowDate().yesterday,
      normal_endtime: getnowDate().endtimer
    };
  },

  created() {
    this.search.start_date = this.normal_startime;
    this.search.end_date = this.normal_endtime;
    this.search.beginStartTime = this.normal_startime;
    this.search.beginEndTime = this.normal_endtime;
    this.search.orveStartTime = this.normal_startime;
    this.search.overEndTime = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_date);
      this.date1.push(this.search.start_date);
      this.date2.push(this.search.start_date);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_date);
      this.date1.push(this.search.end_date);
      this.date2.push(this.search.end_date);
    }
    //页面数据

    this.getTag();
  },
  methods: {
    //页面数据
    getData() {
      let data =
        "&orgAreaId=" +
        this.search.orgAreaId[this.search.orgAreaId.length - 1] +
        "&level=" +
        this.search.level +
        "&startTime=" +
        this.search.start_date +
        "&endTime=" +
        this.search.end_date +
        "&beginStartTime=" +
        this.search.beginStartTime +
        "&beginEndTime=" +
        this.search.beginEndTime +
        "&overStartTime=" +
        this.search.orveStartTime +
        "&overEndTime=" +
        this.search.overEndTime +
        "&pageSize=" +
        this.pageData.pageSize +
        "&currPage=" +
        this.pageData.page;
      getCheckSalesProfit(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.content;
            this.pageData.page = res.data.currentPage;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //获取区域
    getTag() {
      getOrgAreaInfoSelect()
        .then(res => {
          this.treeData = res.data.data;
        })
        .then(() => {
          this.getData();
        });
    },

    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      if (this.date1.length > 0) {
        this.search.beginStartTime = this.date1[0];
        this.search.beginEndTime = this.date1[1];
      }
      if (this.date2.length > 0) {
        this.search.orveStartTime = this.date2[0];
        this.search.overEndTime = this.date2[1];
      }
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },
    //  //选取区域
    handleNodeClick(data) {
      console.log(data);
      this.search.orgAreaId = data[data.length - 1] || "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.date1.push(this.normal_startime);
      this.date1.push(this.normal_endtime);
      this.date2.push(this.normal_startime);
      this.date2.push(this.normal_endtime);
      this.search = {
        start_date: this.normal_startime,
        end_date: this.normal_endtime,
        beginStartTime: this.normal_startime,
        beginEndTime: this.normal_endtime,
        orveStartTime: this.normal_startime,
        overEndTime: this.normal_endtime,
        level: 1,
        orgAreaId: ["2", "3", "4"]
      };
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");

          link.href =
            this.$axios.defaults.baseURLs +
            "/checkController/getCheckSalesProfitReport?token=" +
            getToken() +
            "&orgAreaId=" +
            this.search.orgAreaId[this.search.orgAreaId.length - 1] +
            "&level=" +
            this.search.level +
            "&startTime=" +
            this.search.start_date +
            "&endTime=" +
            this.search.end_date +
            "&beginStartTime=" +
            this.search.beginStartTime +
            "&beginEndTime=" +
            this.search.beginEndTime +
            "&overStartTime=" +
            this.search.orveStartTime +
            "&overEndTime=" +
            this.search.overEndTime;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
